package com.example.chatgpt_test;

import com.example.chatgpt_test.api.CustomChatGpt;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Scanner;

@Slf4j
@SpringBootApplication
public class Test {

    private static String apiKey;

    @Value("${string.apikey}")
    public void setYourValue(String apiKey) {
        Test.apiKey = apiKey;
    }

    public static void main(String[] args) throws IOException {
        SpringApplication.run(Test.class, args);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CustomChatGpt customChatGpt = new CustomChatGpt(apiKey);
        // 根据自己的网络设置
        customChatGpt.setResponseTimeout(20000);
        while (true) {
            System.out.println("q: ");
            String question = new Scanner(System.in).nextLine();
            if ("q".equals(question)) break;
            String answer = customChatGpt.getAnswer(httpClient, question);

            System.out.println("a: ");
            int j = 0;
            for (int i = 0; i < answer.length(); i++, j++) {
                char ch = answer.charAt(i);
                if (ch == '\n') {
                    j = 0;
                }

                if (j != 0 && j % 50 == 0) {
                    System.out.println();
                }

                System.out.print(ch);
            }
            System.out.println();
        }
        httpClient.close();
    }
}
